﻿<html>
<head>

<link href="css/qam.css" rel="stylesheet" media="screen" />
<style>
#power_box{display:inline-block;padding:0 10px;border:#E3E3E3 solid 1px;border-radius:6px;background:#B4D3DE;position:relative;top:10px;}
#power_box label{float:left;display:inline-block;width:100px;}
#power_box input{margin-right:5px;}
.admin{margin-right:10px;}
.red{color:#F00}
</style>

</head>
<body>

	<div style="width:960px;margin:0 auto">
		<div id="message" ></div>
		
		<HR><h3>用户组管理</h3>
		<div style="padding:10px;background:#E8E8FF;margin-top:10px;">
			<form method="post" action="#" name="myform" class="myform">
				<input type="hidden" value="" name="id" />
				<label><span>组名称：</span><input type="text" value="" name="name" /></label><BR />
				<label><span>管理员：</span></label><label class="admin"><input type="radio" value="0"  checked="checked" name="admin" />指定权限</label><label><input type="radio" value="1" name="admin" />拥有所有权限</label><BR />
				<label><span>组权限：</span><div id="power_box"></div></label><BR />
				<input type="button" value="添加" onclick="add();" name="button"  />
			</form>
		</div>
		
		<div style="padding:10px;background:#E8E8FF;margin-top:10px;" id="listbox"></div>
		

	</div>



</body>

<script src="js/ajax.js" type="text/javascript"></script>
<script src="js/md5.js" type="text/javascript"></script>
<script src="js/function.js" type="text/javascript"></script>
<script>


function init(){
	load_power_list();
	showlist();
}

var power_data;
/*加载power列表start*/
function load_power_list(){
	var _url=cgipath+"power-showlist";
	var ajax_obj=new AJAX_OBJ(_url,handle_load_power_list,handle_error,5000);
	ajax_obj.postData();
}

function handle_load_power_list(xmlHttp){
	eval("var ret_json="+xmlHttp.responseText);
	var str="";
	power_data=ret_json.listdata;
	for(var i=0;i<power_data.length;i++){
		str+="<label><input name=\"power\" type=\"checkbox\" value=\""+power_data[i].id+"\" /><a title=\""+power_data[i].info+"\">"+power_data[i].title+"</a></label>";
	}
	$$("power_box").innerHTML=str
	
}
/*加载power列表end*/


/*****group add********/
function add(){
	var _url=cgipath+"group-add";
	var ajax_obj=new AJAX_OBJ(_url,handle_add,handle_error,5000);
	//取得checkbox值
	var power_arr=document.myform.power;
	var power="&power=";
	for(var i=0;i<power_arr.length;i++){
		if(power_arr[i].checked)power+=power_arr[i].value+",";
	}
	//取得radio值
	var admin_arr=document.myform.admin;
	var admin="&admin=";
	for(var i=0;i<admin_arr.length;i++){
		if(admin_arr[i].checked)admin+=admin_arr[i].value+",";
	}
	ajax_obj.postData(formToString(document.myform)+power+admin);
}
function handle_add(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	showlist();
}

/*****group getinfo********/
function edit(t){
	var _url=cgipath+"group-getinfo";
	var ajax_obj=new AJAX_OBJ(_url,handle_edit,handle_error,5000);
	ajax_obj.postData("&id="+t);
}
function handle_edit(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	document.myform.id.value=ret_json.record.id;
	document.myform.name.value=ret_json.record.name;
	for(var i=0;i<document.myform.admin.length;i++){
		if(document.myform.admin[i].value==ret_json.record.admin)document.myform.admin[i].checked="checked"
	}
	
	var power_arr=ret_json.record.power.split(",");
	var str="";
	for(var i=0;i<power_data.length;i++){
		str+="<label><input name=\"power\" type=\"checkbox\" value=\""+power_data[i].id+"\"";
		for(var j=0;j<power_arr.length;j++){
			if(power_data[i].id==power_arr[j]*1)str+=" checked=\"checked\" ";
		}
		str+=" /><a title=\""+power_data[i].info+"\">"+power_data[i].title+"</a></label>";
	}
	
	$$("power_box").innerHTML=str
	
	document.myform.button.value="修改";
	document.myform.button.onclick=Function("update("+ret_json.record.id+")");
}

/*****group update********/
function update(){
	var _url=cgipath+"group-update";
	var ajax_obj=new AJAX_OBJ(_url,handle_update,handle_error,5000);
	//取得checkbox值
	var power_arr=document.myform.power;
	var power="&power=";
	for(var i=0;i<power_arr.length;i++){
		if(power_arr[i].checked)power+=power_arr[i].value+",";
	}
	//取得radio值
	var admin_arr=document.myform.admin;
	var admin="&admin=";
	for(var i=0;i<admin_arr.length;i++){
		if(admin_arr[i].checked)admin+=admin_arr[i].value+",";
	}
	ajax_obj.postData(formToString(document.myform)+admin+power);
}
function handle_update(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	showlist();
}


/*****group del********/

function del(t){
	var _url=cgipath+"group-del";
	var ajax_obj=new AJAX_OBJ(_url,handle_del,handle_error,5000);
	ajax_obj.postData("&id="+t);
}
function handle_del(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	$$("message").innerHTML="【ret】"+ret_json.ret+"【msg】"+ret_json.msg;
	showlist();
}

/*****group list********/
var page=1;//第1页
var reqnum=10;//每页显示10条
var order="id";//排序字段
var desc="1";//反序,0或空正常排序，1反序
var page_nums=0;

function showlist(){
	var _url=cgipath+"group-showlist";
	var ajax_obj=new AJAX_OBJ(_url,handle_showlist,handle_error,5000);
	ajax_obj.postData("&page="+page+"&reqnum="+reqnum+"&order="+order+"&desc="+desc);
}

function handle_showlist(xmlHttp){
	//$$("message").innerHTML=xmlHttp.responseText;return;
	eval("var ret_json="+xmlHttp.responseText);
	var str="<table><thead><tr>";
	str+="<td><a href='javascript:order=\"id\";desc=bin_exchange(desc);list_page(0)'>序号</a></td>";
	str+="<td><a href='javascript:order=\"name\";desc=bin_exchange(desc);list_page(0)'>名称</a></td>";
	str+="<td>超级管理员</td>";
	str+="<td>权限</td>";
	str+="<td>操作</td></tr></thead>";
	for(var i=0;i<ret_json.listdata.length;i++){
		str+="<tr><td>"+((page-1)*reqnum+i+1)+"</td>";
		str+="<td>"+ret_json.listdata[i].name+"</td>";
		
		var power_arr=ret_json.listdata[i].power.split(",");
		var power_str="";
		for(var j=0;j<power_arr.length;j++){
			for(var k=0;k<power_data.length;k++){
				if(power_data[k].id==power_arr[j])power_str+=power_data[k].title+"&nbsp;|&nbsp;";
			}
		}
		var admin_str="指定权限";
		if(ret_json.listdata[i].admin==1) admin_str="<font class=\"red\">拥有所有权限</font>";
		str+="<td>"+admin_str+"</td>";
		str+="<td>"+power_str+"</td>";
		str+="<td><a href='#' onclick='edit("+ret_json.listdata[i].id+")'>修改</a><a href='#' onclick='del("+ret_json.listdata[i].id+")'>删除</a></td></tr>";
	}
	page_nums=Math.ceil(ret_json.nums/reqnum);
	str+="<tr><td colspan=4>共"+ret_json.nums+"条，每页"+reqnum+"条，"+page+"/"+page_nums+"</td>";
	str+="<td><a href='#' onclick='list_page(-1)'>上一页</a><a href='#' onclick='list_page(1)'>下一页</a></td></tr>";
	str+="</table>";
	$$("listbox").innerHTML=str
}

/*列表翻页*/
function list_page(t){
	page=page-(-1)*t;
	if(page<=0)page=1;
	else if(page>page_nums) page=page_nums;
	showlist();
}

</script>
<script src="js/public.js" type="text/javascript"></script>

</html>